package Fixtures.AddCustomer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;

import fit.ActionFixture;

public class AddCustomer extends ActionFixture{
	private String ID;//customer id
	private String name;//customer name
	private String Day;// day of customer birthday
	private String Month;//month of customer birthday
	private String Year;//year of customer birthday
	private String phone;// customers phone
	private String Address;// customer address
	private String regionID;//the number of region
	private String todayDay;
	private String todayMonth;
	private String todayYear;
	private static ArrayList<String> IDlist = new ArrayList<String>();// list of customers already added to the DB
	private String[] regionList = {"100","200","300","400"};// id of region witch are exists in the DB.
	
	public AddCustomer() {
		ID = "";
		name = "";
		Day = "";
		Month = "";
		Year = "";
		phone = "";
		Address = "";
		regionID = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date current = new Date();
		String[] today = new String(sdf.format(current)).split("-");
		todayDay = today[2];
		todayMonth = today[1];
		todayYear = today[0];
	}
/**
 * getter 
 * @return customer id
 */
	public String getId() {
		return ID;
	}
/**
 * setter of customer id
 * @param iD
 */
	public void setId(String iD) {
		ID = iD;
	}
/**
 * getter of customer name
 * @return customer name
 */
	public String getName() {
		return name;
	}
/**
 * setter of customer name
 * @param name
 */
	public void setName(String name) {
		this.name = name;
	}
/**
 * getter of day of the customers birthday
 * @return day of birthday
 */
	public String getDay() {
		return Day;
	}
/**
 * setter of customers day of birthday
 * @param day
 */
	public void setDay(String day) {
		Day = day;
	}
/**
 * getter of month of customers birthday
 * @return month of birthday
 */
	public String getMonth() {
		return Month;
	}
/**
 * setter of  month of customers birthday
 * @param month
 */
	public void setMonth(String month) {
		Month = month;
	}
/**
 * getter of year of customers birthday
 * @return year of birthday
 */
	public String getYear() {
		return Year;
	}
/**
 * setter of  year of customers birthday
 * @param year
 */
	public void setYear(String year) {
		Year = year;
	}
/**
 * getter of customer phone
 * @return customer phone
 */
	public String getPhone() {
		return phone;
	}
/**
 * setter of customer phone
 * @param phone
 */
	public void setPhone(String phone) {
		this.phone = phone;
	}
/**
 * getter of customer address
 * @return customer address
 */
	public String getAddress() {
		return Address;
	}
/**
 *  setter of customer address
 * @param address
 */
	public void setAddress(String address) {
		Address = address;
	}
/**
 * getter of customer region Id
 * @return region id
 */
	public String getRegionId() {
		return regionID;
	}
/**
 * setter of customer region ID
 * @param regionID
 */
	public void setRegionId(String regionID) {
		this.regionID = regionID;
	}

	public String getTodayDay() {
		return todayDay;
	}

	public void setTodayDay(String todayDay) {
		this.todayDay = todayDay;
	}

	public String getTodayMonth() {
		return todayMonth;
	}

	public void setTodayMonth(String todayMonth) {
		this.todayMonth = todayMonth;
	}

	public String getTodayYear() {
		return todayYear;
	}

	public void setTodayYear(String todayYear) {
		this.todayYear = todayYear;
	}
	
	/**
	 * this method check if parameters was inputed correctly
	 * @return true or false
	 */
	public boolean custIsValid(){
	if (ID.isEmpty() || name.isEmpty() || Day.isEmpty() || Month.isEmpty() || Year.isEmpty() ||
			phone.isEmpty() || Address.isEmpty() || regionID.isEmpty())
		return false;
	
	if (isAlpha(name) == false)
		return false;
	if (checkID(ID) == false)
		return false;
	if (isNumeric(ID) == false)
		return false;
	if (isNumeric(phone) == false)
		return false;
	if (isNumeric(regionID) == false)
		return false;
	for(int i=0; i<regionList.length; i++)
	{
	if (regionID.equals(regionList[i]) == true)
		break;
	return false;
	}

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	sdf.setLenient(false);
	String birthday = Year + "-" + Month + "-" + Day;
	try {
		Date birthdayDate = sdf.parse(birthday);
		Date currentDate = new Date();
		String curr = sdf.format(currentDate);
		currentDate = sdf.parse(curr);
		long b = birthdayDate.getTime();
		long c = currentDate.getTime();
		if (b-c > 0)// checking if birthday of customer will be in the future return false
			return false;
	} catch (ParseException e) {
		return false;
	}
	IDlist.add(ID);// adding new cutomer to the array list
	return true;
	
	}
/**
 * this method check if name of customer is not include numbers or characters like#@!!
 * @param name
 * @return boolean
 */
	public boolean isAlpha(String name) {
	    char[] chars = name.toCharArray();

	    for (char c : chars) {
	        if(!Character.isLetter(c) && (!Character.isWhitespace(c))) {
	            return false;
	        }
	    }

	    return true;
	}
	/**
	 * this method check if string include only chars witch we configurate to digits
	 * @param str
	 * @return boolean
	 */
	public boolean isNumeric(String str)
	{
	    for (char c : str.toCharArray())
	    {
	        if (!Character.isDigit(c)) return false;
	    }
	    return true;
	}
	
	/**
	 * this method check if is of customer already exist in the DB
	 * @param ID
	 * @return boolean
	 */
	public boolean checkID (String ID)
	{
		for(int i=0; i<IDlist.size(); i++)
			if (ID.equals(IDlist.get(i)))
				return false;
		return true;
						
	}
}
